<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 * @version		$Id: Template.php 155 2011-04-16 00:55:24Z ninhgio@gmail.com $
 */

class Modules_Mail_Controllers_Template extends Gio_Core_Controller
{
	/* ========== Backend actions =========================================== */

	/**
	 * Add new mail template
	 * 
	 * @return void
	 */
	public function addAction()
	{
		$user    = Gio_Core_Session::getUser();
		$request = $this->getRequest();
		
		if ($request->isPost()) {
			$templateData = $request->getPost('template');
			$templateData = Modules_Mail_Services_Template::validate($templateData);
			
			if (isset($templateData['messages_error']) && $templateData['messages_error']) {
				$this->view->errorMessages = $templateData['messages'];
				$this->view->templateData = $templateData;
				return;
			}
			
			$template = array(
				'name' 			  => $templateData['name'],
				'title' 		  => $templateData['title'],
				'subject' 		  => $templateData['subject'],
				'body' 			  => $templateData['body'],
				'from_mail' 	  => $templateData['from_mail'],
				'from_name' 	  => $templateData['from_name'],
				'reply_to_mail'   => $templateData['reply_to_mail'],
				'reply_to_name'   => $templateData['reply_to_name'],
				'created_user_id' => $user['user_id'],
				'locked'		  => 0,
			);
			
			Modules_Mail_Services_Template::add($template);
			
			Gio_Core_Messenger::getInstance()
								->addMessage($this->view->TRANSLATOR->translator('template_add_success'));
			$this->redirect($this->view->url('mail_template_add'));
		}
		
		$templateData = array(
			'from_name'		=> $user['fullname'],
			'from_mail'	=> $user['email'],
			'reply_to_name' => $user['fullname'],
			'reply_to_mail'=> $user['email'],	 
		);
		$this->view->templateData = $templateData;
	}
	
	/**
	 * Check if the template name exist or not
	 * 
	 * @return void
	 */
	public function checkAction()
	{
		$request  = $this->getRequest();
		$original = $request->getParam('original');
		$name 	  = $request->getParam('name');
		$template = null;
		if ($original == null || ($original != null && $name != $original)) {
			$template = Mail_Services_Template::getByName($name);
		}
		$this->_helper->ajax(($template == null) ? 'true' : 'false');
	}
	
	/**
	 * Delete template
	 * 
	 * @return void
	 */
	public function deleteAction()
	{
		$request = $this->getRequest();
		$id 	 = $request->getPost('id');
		$result  = Mail_Services_Template::delete($id);		
		$this->_helper->ajax($result ? 'RESULT_OK' : 'RESULT_ERROR');
	}
	
	/**
	 * Update mail template
	 * 
	 * @return void
	 */
	public function editAction()
	{
		$user    = Gio_Core_Session::getUser();
		$request = $this->getRequest();
		$templateId = $request->getParam('template_id');
		$template = Modules_Mail_Services_Template::getById($templateId);
		if (null == $template || $user['user_id'] != $template['created_user_id']) {
			Modules_Core_Services_Exception::error('DATA_NOT_FOUND');
		}
		
		$this->view->templateData = $template;
		
		if ($request->isPost()) {
			$templateData = $request->getPost('template');
			$templateData = Modules_Mail_Services_Template::validate($templateData);
			
			if (isset($templateData['messages_error']) && $templateData['messages_error']) {
				$this->view->errorMessages = $templateData['messages'];
				$templateData['template_id'] = $template['template_id'];
				$this->view->templateData = $templateData;
				return;
			}
			
			$template['name'] = $templateData['name'];
			$template['title'] = $templateData['title'];
			$template['subject'] = $templateData['subject'];
			$template['body'] = $templateData['body'];
			$template['from_mail'] = $templateData['from_mail'];
			$template['from_name'] = $templateData['from_name'];
			$template['reply_to_mail'] = $templateData['reply_to_mail'];
			$template['reply_to_name'] = $templateData['reply_to_name'];
			
			Modules_Mail_Services_Template::update($template);
			
			Gio_Core_Messenger::getInstance()
								->addMessage($this->view->TRANSLATOR->translator('template_edit_success'));
			$this->redirect($this->view->url('mail_template_edit', $template));
		}
	}
	
	/**
	 * List mail templates
	 * 
	 * @return void
	 */
	public function listAction()
	{
		$request      = $this->getRequest();
		$pageIndex    = $request->getParam('pageIndex', 1);
		$perPage   	  = 20;
		$offset    	  = ($pageIndex - 1) * $perPage;
		
		$user 		  = Gio_Core_Session::getUser();	
		$templates    = Modules_Mail_Services_Template::getTemplates($user, $offset, $perPage);
		$numTemplates = Modules_Mail_Services_Template::count($user);
		$this->view->templates = $templates; 
		$this->view->numTemplates = $numTemplates; 
		
		// Pager
		require_once LIB_DIR . DS . 'PEAR' . DS . 'Pager' . DS . 'Sliding.php';
		$pagerPath = $this->view->url('mail_template_list');
		$pagerOptions = array(
			'mode'       	=> 'Sliding',
			'append'   		=> false,
    		'perPage'    	=> $perPage,
    		'delta'      	=> 5,
			'urlVar'		=> 'page',
			'path'			=> $pagerPath,
			'fileName'		=> 'pag-/%d',
			'separator'		=> '',
			'nextImg' 		=> '<small class="icon arrow_right"></small>',
			'prevImg'		=> '<small class="icon arrow_left"></small>',
			'altNext'		=> '',
			'altPrev'		=> '',
			'altPage'		=> '',
    		'totalItems' 	=> $numTemplates,
			'currentPage'	=> $pageIndex,
			'urlSeparator'	=> '/',
			'spacesBeforeSeparator' => 0,
			'spacesAfterSeparator' => 0,
			'curPageSpanPre' => '<a href="javascript: void();" class="current">',
			'curPageSpanPost' => '</a>'
		
		);
		$pager = new Pager_Sliding($pagerOptions);
		$this->view->pager = $pager;
	}
}
